package com.flea.hclv.service.impl;

import com.flea.hclv.constant.Const;
import com.flea.hclv.model.dao.HPartnershipMapper;
import com.flea.hclv.model.entity.HPartnership;
import com.flea.hclv.service.HPartnershipService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: project
 * @description: 组团社与供应商的合作关系(暂时没用这个类)
 * @author: shiliqiang
 * @create: 2018-11-02 19:20
 * @version: 1.0
 */
@Service
public class HPartnershipServiceImpl implements HPartnershipService{

    @Autowired
    private HPartnershipMapper hPartnershipMapper;

    @Override
    public ServerResponse<String> updateStatus(String userId, String partnershipId, String status) {
        if (partnershipId==null||status==null){
            return ServerResponse.createByErrorMessage("参数未找到");
        }
        HPartnership hPartnership = new HPartnership();
        hPartnership.setId(partnershipId);
        hPartnership.setStatus(status);
        int row = hPartnershipMapper.updateByPrimaryKeySelective(hPartnership);
        if (row>0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse<String> save(String userId, OrgDept currentUserDept, String receiveId) {
        if (currentUserDept ==null){
            return ServerResponse.createByErrorMessage("部门ID不能为空");
        }

        if (receiveId ==null){
            return ServerResponse.createByErrorMessage("接收方Id不能是空");
        }
        //这里的通过receiveId 接收人ID 获取部门，表中存储部门id
        OrgDept dept = FleaOrgUtils.getCurrentUserDept(receiveId);
        if (dept ==null){
            return ServerResponse.createByErrorMessage("接收方部门不能是空");
        }
        String receiveDeptId = dept.getDeptUuid();
        HPartnership hPartnership = new HPartnership();
        hPartnership.setId(FleaStringUtils.getGenerateUUID());
        hPartnership.setCreateUser(userId);
        hPartnership.setModifyUser(userId);
        hPartnership.setReceiveSide(receiveDeptId);
        hPartnership.setSendSide(currentUserDept.getDeptUuid());
        // NOT_ESTABLISHED 未建立
        hPartnership.setStatus(Const.PartnershipStatusEnum.NOT_ESTABLISHED.getCode());
        int row = hPartnershipMapper.insertSelective(hPartnership);
        if (row>0){
            return ServerResponse.createBySuccess("发送成功");
        }
        return ServerResponse.createByErrorMessage("发送失败");
    }
}
